package com.hdoit.ares.web.share.wechat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IGoldenBeanDetailService;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.acti.model.Activity;
import com.hdoit.ares.logical.acti.service.IActivityService;
import com.hdoit.ares.logical.cust.model.CustCollection;
import com.hdoit.ares.logical.cust.model.CustFootprint;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustCollectionService;
import com.hdoit.ares.logical.cust.service.ICustFootprintService;
import com.hdoit.ares.logical.official.model.Artical;
import com.hdoit.ares.logical.official.model.ArticalClassify;
import com.hdoit.ares.logical.official.model.ArticalComment;
import com.hdoit.ares.logical.official.model.ArticalPraise;
import com.hdoit.ares.logical.official.model.PraiseAndGlance;
import com.hdoit.ares.logical.official.service.IArticalClassifyService;
import com.hdoit.ares.logical.official.service.IArticalCommentService;
import com.hdoit.ares.logical.official.service.IArticalPraiseService;
import com.hdoit.ares.logical.official.service.IArticalService;
import com.hdoit.ares.logical.official.service.IPraiseAndGlanceService;
import com.hdoit.ares.logical.share.service.IConsultService;
import com.hdoit.ares.logical.share.service.IShareStudyService;
import com.hdoit.ares.logical.viewmodel.official.ArticalClassifySearch;
import com.hdoit.ares.logical.viewmodel.official.ArticalCommentSearch;
import com.hdoit.ares.logical.viewmodel.official.ArticalSearch;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.hdoit.ares.selfcommon.enums.official.TypeOfPraise;
import com.hdoit.ares.selfcommon.utils.EmojiUtil;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.JsonUtils;
import com.tennetcn.component.wechat.BaseWechat;
import com.tennetcn.web.cbase.WechatAuthPassport;
import com.tennetcn.web.cbase.WechatBrowserAuthPassport;

@Controller
@RequestMapping(value="/wechat/share/")
public class ShareWechat extends BaseWechat{

	@Override
	public String getRoutePath() {
		
		return "areas/wechat/share/";
	}
	
	private IArticalService articalService;
	private IArticalClassifyService articalClassifyService;
	private IPraiseAndGlanceService praiseAndGlanceService;
	private IArticalCommentService articalCommentService;
	private IArticalPraiseService articalPraiseService;
	private IWithdrawParamService withdrawParamService;

	private IActivityService activityService;
	private IGoldenBeanDetailService goldenBeanDetailService;

	private ICustFootprintService custFootprintService;
	private ICustCollectionService custCollectionService;
	
	private IShareStudyService shareStudyService;
	public IShareStudyService getShareStudyService() {
		return shareStudyService;
	}
	@Resource
	public void setShareStudyService(IShareStudyService shareStudyService) {
		this.shareStudyService = shareStudyService;
	}

	private IConsultService consultService;
	public IConsultService getConsultService() {
		return consultService;
	}
	@Resource
	public void setConsultService(IConsultService consultService) {
		this.consultService = consultService;
	}
	public ICustCollectionService getCustCollectionService() {
		return custCollectionService;
	}
	@Resource
	public void setCustCollectionService(
			ICustCollectionService custCollectionService) {
		this.custCollectionService = custCollectionService;
	}
	public ICustFootprintService getCustFootprintService() {
		return custFootprintService;
	}
	@Resource
	public void setCustFootprintService(ICustFootprintService custFootprintService) {
		this.custFootprintService = custFootprintService;
	}
	public IArticalService getArticalService() {
		return articalService;
	}
	@Resource
	public void setArticalService(IArticalService articalService) {
		this.articalService = articalService;
	}
	
	

	public IArticalClassifyService getArticalClassifyService() {
		return articalClassifyService;
	}
	@Resource
	public void setArticalClassifyService(IArticalClassifyService articalClassifyService) {
		this.articalClassifyService = articalClassifyService;
	}


	public IPraiseAndGlanceService getPraiseAndGlanceService() {
		return praiseAndGlanceService;
	}
	@Resource
	public void setPraiseAndGlanceService(IPraiseAndGlanceService praiseAndGlanceService) {
		this.praiseAndGlanceService = praiseAndGlanceService;
	}

	
	public IArticalCommentService getArticalCommentService() {
		return articalCommentService;
	}
	@Resource
	public void setArticalCommentService(IArticalCommentService articalCommentService) {
		this.articalCommentService = articalCommentService;
	}
	
	public IArticalPraiseService getArticalPraiseService() {
		return articalPraiseService;
	}
	@Resource
	public void setArticalPraiseService(IArticalPraiseService articalPraiseService) {
		this.articalPraiseService = articalPraiseService;
	}

	
	public IActivityService getActivityService() {
		return activityService;
	}
	@Resource
	public void setActivityService(IActivityService activityService) {
		this.activityService = activityService;
	}

    
	public IGoldenBeanDetailService getGoldenBeanDetailService() {
		return goldenBeanDetailService;
	}
	@Resource
	public void setGoldenBeanDetailService(IGoldenBeanDetailService goldenBeanDetailService) {
		this.goldenBeanDetailService = goldenBeanDetailService;
	}
	
	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}
	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	private final String cloudClassViewName="cloudClass";
	private final String adDetailsViewName="adDetails";
	private final String informationViewName="information";
	private final String informationDetailViewName="informationDetail";
	private final String videoDetailViewName="videoDetail";
	private final String articalThumbViewName="articalThumb";
	private final String commentThumbViewName="commentThumb";
	private final String pullUpLoadingViewName="pullUpLoading";
	private final String postMessageViewName="postMessage";
	private final String commentDetailViewName="commentDetail";
	private final String articalDetailViewName="articalDetail";
	private final String consultViewName="consult";
	private final String toStudyViewName="toStudy";
	
	@RequestMapping(value = cloudClassViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String cloudClass(Model model){
		
		List<ArticalClassify> articalClassifys = articalClassifyService.queryList(view(cloudClassViewName));
		articalClassifys = sortForArticalClassify(articalClassifys);
		//找出热点资讯
		List<Artical> articals = articalService.findHot(articalClassifys);
		
		
		//找出成功分享的资讯
		ArticalClassifySearch articalClassifySearch = new ArticalClassifySearch();
		articalClassifySearch.setFunctionURL(view(informationViewName) + "/successShare");
		ArticalClassify articalClassify = articalClassifyService.queryModel(articalClassifySearch);
		
		ArticalSearch articalSearch = new ArticalSearch();
		articalSearch.setTypeId(articalClassify.getId());
		Artical artical = articalService.queryList(articalSearch).get(0);
		
		//活动任务轮播
		List<Activity> activitys = activityService.queryListHotActivitys();
		
		model.addAttribute("activitys", activitys);
		model.addAttribute("articalClassifys", articalClassifys);
		model.addAttribute("articals", articals);
		model.addAttribute("artical", artical);
		return view(cloudClassViewName);
	}
	
	@RequestMapping(value = adDetailsViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String adDetails(Model model){
		return view(adDetailsViewName);
	}
	
	@WechatBrowserAuthPassport
	@WechatAuthPassport
	@RequestMapping(value = articalDetailViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String articalDetail(Model model,String id){
		
		Artical artical = articalService.queryModel(id);
		
		model.addAttribute("artical", artical);
		return view(articalDetailViewName);
		
	}
	
	
	@RequestMapping(value = informationDetailViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String informationDetail(Model model,String id){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		
		
		Artical artical = articalService.queryModel(id);
		//点赞次数和浏览次数
		PraiseAndGlance praiseAndGlance = praiseAndGlanceService.queryModel(artical.getId(), TypeOfPraise.Medio);
		praiseAndGlance.setGlanceCount(praiseAndGlance.getGlanceCount() + 1);
		System.err.println("检测是否真的发生了变化" + praiseAndGlance.getGlanceCount());
		
		praiseAndGlanceService.updateModel(praiseAndGlance);
		
		//是否点过赞
		ArticalPraise articalPraise = articalPraiseService.queryModel(id,TypeOfPraise.Medio,customer.getId());
		
		//评论
		WithdrawParam withdrawParam = withdrawParamService.queryModelByHelpCode("pageSize");
		
		ArticalCommentSearch search = new ArticalCommentSearch();
		search.setArticalId(id);
		search.setPraiseCutomerId(customer.getId());
		search.setPageSize(withdrawParam.getParam());
		List<ArticalComment> articalComments = articalCommentService.queryListBySearch(search);
		if(articalComments != null){
			for(ArticalComment articalComment:articalComments){
				articalComment.setComment(EmojiUtil.emojiConverterUnicodeStr(articalComment.getComment()));
			}
		}
		
		
		/*收藏*/
		CustCollection custCollection=custCollectionService.selectCustCollection(artical.getId(), customer.getId());
		model.addAttribute("custCollection",custCollection);
		
		//足迹
		CustFootprint custFootprint=custFootprintService.selectCustFootprint(artical.getId(), customer.getId());
		if(custFootprint == null){
			custFootprint = new CustFootprint();
			custFootprint.setId(CommonUtils.getKeyId());
			custFootprint.setType(artical.getMedioType());
			custFootprint.setThingId(artical.getId());
			custFootprint.setCustId(customer.getId());
			custFootprint.setBrowseDate(DateUtils.getCurrent());
			
			custFootprint.setModelStatus(ModelStatus.add);
		}else{
			custFootprint.setBrowseDate(DateUtils.getCurrent());
			
			custFootprint.setModelStatus(ModelStatus.update);
		}
		custFootprintService.applyChange(custFootprint);
		
		model.addAttribute("customer", customer);
		model.addAttribute("articalComments", articalComments);
		model.addAttribute("articalPraise", articalPraise);
		model.addAttribute("artical", artical);
		model.addAttribute("praiseAndGlance", praiseAndGlance);
		return view(informationDetailViewName);
	}
	
	
	@RequestMapping(value = videoDetailViewName, method = { RequestMethod.GET,RequestMethod.POST },produces = "text/plain;charset=utf-8")
	public String videoDetail(Model model,String id){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		
		
		Artical artical = articalService.queryModel(id);
		//点赞次数和浏览次数
		PraiseAndGlance praiseAndGlance = praiseAndGlanceService.queryModel(artical.getId(), TypeOfPraise.Medio);
		praiseAndGlance.setGlanceCount(praiseAndGlance.getGlanceCount() + 1);
		System.err.println("检测是否真的发生了变化" + praiseAndGlance.getGlanceCount());
		
		praiseAndGlanceService.updateModel(praiseAndGlance);
		
		//是否点过赞
		ArticalPraise articalPraise = articalPraiseService.queryModel(id,TypeOfPraise.Medio,customer.getId());
		
		//评论
		ArticalCommentSearch search = new ArticalCommentSearch();
		search.setArticalId(id);
		search.setPraiseCutomerId(customer.getId());
		List<ArticalComment> articalComments = articalCommentService.queryListBySearch(search);
		if(articalComments != null){
			for(ArticalComment articalComment:articalComments){
				articalComment.setComment(EmojiUtil.emojiConverterUnicodeStr(articalComment.getComment()));
			}
		}
		
		
		
		/*收藏*/
		CustCollection custCollection=custCollectionService.selectCustCollection(artical.getId(), customer.getId());
		model.addAttribute("custCollection",custCollection);
		
		//足迹
		CustFootprint custFootprint=custFootprintService.selectCustFootprint(artical.getId(), customer.getId());
		if(custFootprint == null){
			custFootprint =new CustFootprint();
			custFootprint.setId(CommonUtils.getKeyId());
			custFootprint.setType(artical.getMedioType());
			custFootprint.setThingId(artical.getId());
			custFootprint.setCustId(customer.getId());
			custFootprint.setBrowseDate(DateUtils.getCurrent());
					
			custFootprint.setModelStatus(ModelStatus.add);
		}else{
			custFootprint.setBrowseDate(DateUtils.getCurrent());
					
			custFootprint.setModelStatus(ModelStatus.update);
		}
			custFootprintService.applyChange(custFootprint);
			
		model.addAttribute("customer", customer);
		model.addAttribute("articalComments", articalComments);
		model.addAttribute("articalPraise", articalPraise);
		model.addAttribute("artical", artical);
		model.addAttribute("praiseAndGlance", praiseAndGlance);
		return view(videoDetailViewName);
	}
	
	
	
	@ResponseBody
	@RequestMapping(value = postMessageViewName, method = { RequestMethod.GET,RequestMethod.POST },produces = "text/plain;charset=utf-8")
	public String postMessage(Model model,String content,String articalId,String replyedId){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		return JsonUtils.toJson(articalCommentService.addModel(EmojiUtil.emojiConverterToAlias(content), articalId, customer.getId(),replyedId)) ;
	}
	
	@ResponseBody
	@RequestMapping(value = articalThumbViewName, method = { RequestMethod.GET,RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String articalThumb(Model model,String articalId){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);

	    return articalPraiseService.toThumb(articalId.trim(),TypeOfPraise.Medio,customer.getId());
		
	}
	
	@ResponseBody
	@RequestMapping(value = commentThumbViewName, method = { RequestMethod.GET,RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String commentThumb(Model model,String commentId){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);

		return articalPraiseService.toThumb(commentId.trim(),TypeOfPraise.Comment,customer.getId());
		
	}
	
	@ResponseBody
	@RequestMapping(value = pullUpLoadingViewName, method = { RequestMethod.GET,RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String pullUpLoading(Model model,String beforeTime,String articalId){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		
		WithdrawParam withdrawParam = withdrawParamService.queryModelByHelpCode("pageSize");
		
		System.err.println("时间" + beforeTime);
		ArticalCommentSearch search = new ArticalCommentSearch();
		search.setArticalId(articalId);
		search.setBeforeTime(DateUtils.parseDateTime(beforeTime));
		search.setPageSize(withdrawParam.getParam());
		search.setPraiseCutomerId(customer.getId());
		
		List<ArticalComment> articalComments = articalCommentService.queryListBySearch(search);
		if(articalComments != null){
			for(ArticalComment articalComment:articalComments){
				articalComment.setComment(EmojiUtil.emojiConverterUnicodeStr(articalComment.getComment()));
			}
		}else{
			articalComments = new ArrayList<ArticalComment>();
		}
		
		/*ResponseModel rm = new ResponseModel();
		List<String> articalCommentParams = new GetClassPropertyName<ArticalComment>().getClassName(new ArticalComment());
		rm.putList("articalComments", articalComments, articalCommentParams);*/

		return JSONObject.toJSONStringWithDateFormat(articalComments, "yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteDateUseDateFormat);
		
	}
	
	
	@RequestMapping(value = informationViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String information(Model model,String picture,String name){
		ArticalClassifySearch articalClassifySearch = new ArticalClassifySearch();
		articalClassifySearch.setFunctionURL(view(informationViewName) + "/" + name);
		ArticalClassify articalClassify = articalClassifyService.queryModel(articalClassifySearch);
		
		WithdrawParam withdrawParam = withdrawParamService.queryModelByHelpCode("pageSize");
		
		
		ArticalSearch articalSearch = new ArticalSearch();
		articalSearch.setPageSize(withdrawParam.getParam());
		articalSearch.setTypeId(articalClassify.getId());
		List<Artical> articals = articalService.queryArticals(articalSearch);
		
		model.addAttribute("name", name);
		model.addAttribute("picture", picture);
		model.addAttribute("articals", articals);
		
		return view(informationViewName);
	}
	
	//评论详情
	@RequestMapping(value = commentDetailViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String commentDetail(Model model,String commentId,String type){
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);

		System.err.println("万万没想到" + customer.getId());
		
		ArticalCommentSearch articalCommentSearch = new ArticalCommentSearch();
		articalCommentSearch.setId(commentId);
		articalCommentSearch.setPraiseCutomerId(customer.getId());
		ArticalComment articalComment = articalCommentService.queryModel(articalCommentSearch);
		
		articalComment.setComment(EmojiUtil.emojiConverterUnicodeStr(articalComment.getComment()));
		
		model.addAttribute("type", type);
		model.addAttribute("articalComment", articalComment);
		return view(commentDetailViewName);
	}
	
	
	
	 //排序
    public List<ArticalClassify> sortForArticalClassify(List<ArticalClassify> articalClassifys){
    	if(articalClassifys != null && articalClassifys.size() >= 2){
    		/*Collections.sort(articalClassifys, new Comparator<ArticalClassify>(){
				@Override
				public int compare(ArticalClassify o1, ArticalClassify o2) {	
					return o1.getSortCode() - o2.getSortCode();
				}	
    		});*/
    		//与上面等价，lambda表达式处理
    		Collections.sort(articalClassifys, Comparator.comparing(ArticalClassify::getSortCode));
    	}
    	return articalClassifys;
    }
    
    @ResponseBody
    @RequestMapping(value = consultViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public void consult(Model model,String shareId,String shareType){
    	
    	/*if(!StringUtils.isEmpty(shareId) && !StringUtils.isEmpty(shareType)){
    		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
    		consultService.setConsult(shareId, shareType, loginModel.getLoginId());
    	}*/
    }
    
    @ResponseBody
    @RequestMapping(value = toStudyViewName, method = { RequestMethod.GET,RequestMethod.POST }, produces = "text/plain;charset=utf-8")
	public String toStudy(Model model,String shareId,String shareType){
    	/*String result = "1";
    	if(!StringUtils.isEmpty(shareId) &&"video".equals(shareType)){
    		System.err.println("进来了");
    		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
    		if(shareStudyService.setStudyVideos(shareId, shareType, loginModel.getLoginId())){
    			result ="0";
    		}
    	}*/
    	return "";//result;
    }
}
